package weaver.interfaces.jz.job;

import com.alibaba.fastjson.JSONObject;
import com.engine.jz.util.CommonUtil;
import com.engine.jz.util.KsUtils;
import com.engine.jz.util.TelpoUtils;
import com.engine.kq.biz.KQFormatBiz;
import com.engine.kq.wfset.util.SplitActionUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.interfaces.schedule.BaseCronJob;

import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;

/**
 * @version 1.0
 * @Title ecology-9
 * @Company 泛微软件
 * @CreateDate 2024/12/02
 * @Description 同步天波门禁数据
 * @Author AdminZm
 */
public class SyncTbDoorDataJob extends BaseCronJob {

    BaseBean baseBean = new BaseBean();

    @Override
    public void execute() {
        baseBean.writeLog("SyncTbDoorDataJob start.");
        try {
            LocalDate currentDate = LocalDate.now();
            // 获取前一天的日期
            LocalDate previousDate = currentDate.minusDays(1);
            java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd");
            String formattedDate = previousDate.format(formatter);
            String startTime = formattedDate + " 00:00:01";
            String endTime = formattedDate + " 23:59:59";

            RecordSet rs = new RecordSet();
            RecordSet rs1 = new RecordSet();

            // 获取工号与id对应
            Map<String, String> ghAndIdMap = new HashMap<>();
            rs.execute("SELECT id, loginid FROM hrmresource WHERE workcode is not null");
            while (rs.next()) {
                String id = rs.getString("id");
                String workcode = rs.getString("loginid");
                if (StringUtils.isEmpty(id) || StringUtils.isEmpty(workcode)) {
                    continue;
                }
                ghAndIdMap.put(workcode, id);
            }
            if (CollectionUtils.isEmpty(ghAndIdMap)) {
                baseBean.writeLog("SyncKsDoorDataJob ghAndIdMap is null.");
                return;
            }

            String modeId = rs.getPropValue("jzjg_zm_prop", "kqsjtbjl_mode_id");
            String cusId = rs.getPropValue("jzjg_zm_prop", "kqsjtbjl_cus_id");
            SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat sdfTime = new SimpleDateFormat("HH:mm:ss");

            TelpoUtils telpoUtils = new TelpoUtils();

            String sbip = rs.getPropValue("jzjg_zm_prop", "tb_ip");
            // 获取天波考勤设备ip
            rs.execute("SELECT * FROM uf_kqsbdzgxgljmb WHERE sblx = 0 AND sbip is not null AND sftskqsj = 0");
            while (rs.next()) {
                String id = rs.getString("id");
//                String sbip = "192.168.1.43";
                String sbxlh = rs.getString("sbxlh");
                if (StringUtils.isEmpty(id) || StringUtils.isEmpty(sbip) || StringUtils.isEmpty(sbxlh)) {
                    continue;
                }
                Map<String, Object> paramMap = new HashMap<>();
                List<String> snList = new ArrayList<>();
                snList.add(sbxlh);
                paramMap.put("SN", snList);
                long taskId = System.currentTimeMillis();
                paramMap.put("taskId", taskId);
                paramMap.put("type", "queryRecords");
                Map<String, Object> mapTemp = new HashMap<>();
                mapTemp.put("photo", 0);
                mapTemp.put("start", startTime);
                mapTemp.put("end", endTime);
                paramMap.put("Data", mapTemp);
                rs1.executeUpdate("INSERT INTO uf_kqsjtbjlb (sb, rwid, qqcs, formmodeid,MODEUUID,modedatacreater," +
                        "modedatacreatertype, modedatacreatedate, modedatacreatetime) VALUES (?,?,?,?,?,?,?,?,?)", id, taskId,
                        JSONObject.toJSONString(paramMap), modeId, UUID.randomUUID(), "1", "0", sdfDate.format(new Date()), sdfTime.format(new Date()));
                telpoUtils.queryTelpoMj(sbip, paramMap, String.valueOf(taskId));
            }
        } catch (Exception e) {
            baseBean.writeLog("SyncTbDoorDataJob error:" + e);
        }
        baseBean.writeLog("SyncTbDoorDataJob end.");
    }
}
